package cn.fl.institution.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstNpBscInfoService;
import cn.fl.customer.vo.CstNpBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.condition.PrjCstAccInfoQueryCondition;
import cn.fl.project.service.IPrjCstAccInfoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 江西裕民银行信息采集
 */
@RestController
@RequestMapping(value = "/institution/jxymbank")
public class PrjBscInfoInsJxymbankController {

    /**
     * 自然人信息
     */
    @Autowired
    private ICstNpBscInfoService cstNpBscInfoService;

    /**
     * 客户账户信息
     */
    @Autowired
    private IPrjCstAccInfoService cstAccInfoService;

    /**
      * @Author yangzhongkun
      * @params [request]
      * @Description 保存资方信息采集 信息
      * @Date 2020/7/6 18:17
      * @return cmm.mid.core.framework.page.DataResultVO
      **/
    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    @ResponseBody
    public DataResultVO save(HttpServletRequest request) throws BaseException {

        BaseLogUtils.newLogger("江西裕民银行信息采集保存Start:request={},currentUserName={}",JSON.toJSONString(request.getParameterMap()), CurrentThreadContext.getCurrentUserName()).info();
        PrjCstAccInfoQueryCondition prjCstAccInfoQC = new PrjCstAccInfoQueryCondition();
        DataResultVO resultVO = null;
        try{
            //1. 组装请求参数
            resultVO = this.packrjCstAccInfo(prjCstAccInfoQC,request);
            if(null != resultVO){
                return resultVO;
            }

            //2. 保存修改自然人信息
            this.modifyCstNpBscInfo(prjCstAccInfoQC);
            //3. 保存修改客户账户信息
            resultVO = this.modifyCstAccInfo(prjCstAccInfoQC);
            //4. 四要素校验
            if(resultVO.getSuccess() == BaseConstant.IS_YESNO_NO) {
                resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                resultVO.setInfo(resultVO.getInfo());
            }
            if (BaseConstant.IS_YESNO_YES == resultVO.getSuccess()) {
                Map<String,String> resultMap = (Map<String, String>) resultVO.getData();
                if ("1".equals(resultMap.get("bankCardType"))){
                    resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                    resultVO.setInfo("不支持此类银行卡，请更换为借记卡");
                    return resultVO;
                }
                if ("1".equals(resultMap.get("chkRstCd"))) {
                    resultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                    resultVO.setInfo("四要素信息校验通过");
                } else {
                    resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                    resultVO.setInfo("四要素信息有误，请核实姓名、身份证号、银行卡账号和银行预留手机号一致");
                }
            }
        }catch (BaseException ex) {
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo(ex.getMessage());
            BaseLogUtils.newLogger("江西裕民银行信息采集保存自然人信息或项目客户账户信息及校验失败,prjId={},errorResult={}",prjCstAccInfoQC.getPrjId(),ex.getMessage()).info();
            BaseLogUtils.newLogger("江西裕民银行信息采集保存自然人信息或项目客户账户信息及校验失败,prjId={},errorResult={}",prjCstAccInfoQC.getPrjId(),ex.getMessage()).error(BaseExceptionConstant.INTERFACE_0102);

        } catch (Exception e) {
            BaseLogUtils.newLogger("江西裕民银行信息采集保存自然人信息或项目客户账户信息及校验失败,prjId={},errorResult={}",prjCstAccInfoQC.getPrjId(),e.getMessage()).info();
            BaseLogUtils.newLogger("江西裕民银行信息采集保存自然人信息或项目客户账户信息及校验失败,prjId={},errorResult={}",prjCstAccInfoQC.getPrjId(),e.getMessage()).error(BaseExceptionConstant.INTERFACE_0102);
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "保存失败，系统异常");
        }
        BaseLogUtils.newLogger("江西裕民银行信息采集保存信息及校验完成End:prjId={},resultVO={}",prjCstAccInfoQC.getPrjId(),JSON.toJSONString(resultVO)).info();
        return resultVO;
    }


    /**
      * @Author yangzhongkun
      * @params [prjCstAccInfoQC]
      * @Description 保存客户账户信息
      * @Date 2020/7/6 20:54
      * @return cmm.mid.core.framework.page.DataResultVO
      **/
    private DataResultVO modifyCstAccInfo(PrjCstAccInfoQueryCondition prjCstAccInfoQC) throws BaseException{
        DataResultVO dataResultVO = null;
        try{
            prjCstAccInfoQC.setCertTypCd(DictionaryConstant.CODE_CERT_TYP_CD_0);//证件类型
            BaseLogUtils.newLogger("江西裕民银行信息采集保存或修改客户账户信息Start:prjCstAccInfoQC={}",JSONObject.toJSONString(prjCstAccInfoQC)).info();
            dataResultVO = this.cstAccInfoService.modifyPrjAccInfo(prjCstAccInfoQC);
            BaseLogUtils.newLogger("江西裕民银行信息采集保存或修改客户账户信息End:prjId={},dataResultVO={}",prjCstAccInfoQC.getPrjId(),JSONObject.toJSONString(dataResultVO)).info();
        }catch (BaseException ex) {
            BaseLogUtils.newLogger("江西裕民银行信息采集保存客户账户信息异常:prjCstAccInfoQC={},Error={}",JSONObject.toJSONString(prjCstAccInfoQC),ex.getMessage()).info();
            BaseLogUtils.newLogger("江西裕民银行信息采集保存客户账户信息异常:prjCstAccInfoQC={},Error={}",JSONObject.toJSONString(prjCstAccInfoQC),ex.getMessage()).error(BaseExceptionConstant.COMMON_0001);
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户账户信息保存失败!");
        }
        return dataResultVO;
    }

    /**
      * @Author yangzhongkun
      * @params [prjCstAccInfoQC]
      * @Description 修改自然人信息
      * @Date 2020/7/6 20:33
      * @return void
      **/
    private void modifyCstNpBscInfo(PrjCstAccInfoQueryCondition prjCstAccInfoQC) throws BaseException {

        //根据客户id查询 客户信息
        BaseLogUtils.newLogger("江西裕民银行根据cstId查询自然人信息Start:cstId={}",prjCstAccInfoQC.getCstId()).info();
        CstNpBscInfoVO cstNpBscInfoVO = this.cstNpBscInfoService.selectById(prjCstAccInfoQC.getCstId());
        BaseLogUtils.newLogger("江西裕民银行根据cstId查询自然人信息End:cstId={},cstNpBscInfoVO={}",prjCstAccInfoQC.getCstId(),JSONObject.toJSONString(cstNpBscInfoVO)).info();
        if(null != cstNpBscInfoVO){
            //修改标识
            boolean flag = false;
            CstNpBscInfoVO npBscInfoVO = new CstNpBscInfoVO();
            npBscInfoVO.setId(cstNpBscInfoVO.getId());
            //证件有效起始日期
            if(!Objects.equals(prjCstAccInfoQC.getCertValidSDt(),cstNpBscInfoVO.getCertValidSDt())){
                npBscInfoVO.setCertValidSDt(prjCstAccInfoQC.getCertValidSDt());
                flag = true;
            }
            //证件有效终止日期
            if(!Objects.equals(prjCstAccInfoQC.getCertValidEDt(),cstNpBscInfoVO.getCertValidEDt())){
                npBscInfoVO.setCertValidEDt(prjCstAccInfoQC.getCertValidEDt());
                flag = true;
            }
            //有信息变更 更新变更时间和变更人
            if(flag){
                npBscInfoVO.setMdfTm(new Date());//更新时间
                npBscInfoVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());//更新人
            }
            BaseLogUtils.newLogger("江西裕民银行修改自然人信息Start:cstId={},npBscInfoVO={}",prjCstAccInfoQC.getCstId(),JSON.toJSONString(npBscInfoVO)).info();
            boolean modify = this.cstNpBscInfoService.modify(npBscInfoVO);
            BaseLogUtils.newLogger("江西裕民银行修改自然人信息End:cstId={},modifyResult={},npBscInfoVO={}",prjCstAccInfoQC.getCstId(),modify,JSON.toJSONString(npBscInfoVO)).info();
        }

    }

    /**
      * @Author yangzhongkun
      * @params [prjCstAccInfoQC, request]
      * @Description 组装请求参数
      * @Date 2020/7/6 20:54
      * @return cmm.mid.core.framework.page.DataResultVO
      **/
    private DataResultVO packrjCstAccInfo(PrjCstAccInfoQueryCondition prjCstAccInfoQC,HttpServletRequest request){

        try{
            //项目客户信息表id
            String cstAccInfoId = request.getParameter("cstAccInfoId");
            //项目id
            String prjId = request.getParameter("prjId");
            //客户id
            String cstId = request.getParameter("cstId");
            //客户名称
            String cstNm = request.getParameter("cstNm");
            //机构code
            String insCode = request.getParameter("insCode");
            //机构id
            String fundRasInsId = request.getParameter("fundRasInsId");
            //证件号码
            String certNo = request.getParameter("certNo");
            //证件有效起始日期
            String certValidSDt = request.getParameter("certValidSDt");
            //证件有效终止日期
            String certValidEDt = request.getParameter("certValidEDt");
            //银行代码
            String bkCd = request.getParameter("bkCd");
            //银行名称
            String bkNm = request.getParameter("bkNm");
            //银行卡号
            String accNo = request.getParameter("accNo");
            //银行预留手机号
            String mp = request.getParameter("mp");

            if(StringUtils.isNotEmpty(cstAccInfoId)) {
                prjCstAccInfoQC.setId(Long.valueOf(cstAccInfoId));
            }
            if(StringUtils.isNotEmpty(prjId)) {
                prjCstAccInfoQC.setPrjId(Long.valueOf(prjId));
            }
            if(StringUtils.isNotEmpty(cstId)) {
                prjCstAccInfoQC.setCstId(Long.valueOf(cstId));
            }
            if(StringUtils.isNotEmpty(fundRasInsId)){
                prjCstAccInfoQC.setInsId(Long.valueOf(fundRasInsId));
            }

            prjCstAccInfoQC.setCstNm(cstNm);
            prjCstAccInfoQC.setInsCode(insCode);
            prjCstAccInfoQC.setCertNo(certNo);
            prjCstAccInfoQC.setCertTypCd(DictionaryConstant.CODE_CERT_TYP_CD_0);//证件类型
            prjCstAccInfoQC.setCertValidSDt(certValidSDt);
            prjCstAccInfoQC.setCertValidEDt(certValidEDt);
            prjCstAccInfoQC.setBkNo(bkCd);
            prjCstAccInfoQC.setBkCd(bkCd);
            prjCstAccInfoQC.setBkNm(bkNm);
            prjCstAccInfoQC.setAccNo(accNo);
            prjCstAccInfoQC.setMp(mp);
            BaseLogUtils.newLogger("江西裕民银行信息采集组装采集信息完成：prjCstAccInfoQC={}",JSON.toJSONString(prjCstAccInfoQC)).info();
        }catch (BaseException ex) {
            BaseLogUtils.newLogger("江西裕民银行信息保存采集请求参数异常:prjCstAccInfoQC={},Error={}",JSONObject.toJSONString(prjCstAccInfoQC),ex.getMessage()).info();
            BaseLogUtils.newLogger("江西裕民银行信息保存采集请求参数异常:prjCstAccInfoQC={},Error={}",JSONObject.toJSONString(prjCstAccInfoQC),ex.getMessage()).error(BaseExceptionConstant.COMMON_0001);
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "参数请求异常!");
        }
        return null;
    }

}
